<?php
/**
 * 日付リクエストクラスモジュール
 */
require_once dirname(__FILE__) . '/Request2.php';

/**
 * 日付リクエストクラス
 * Enter description here ...
 * @author admin
 *
 */
class Lib_Request_DateTime extends Lib_Request2
{
	/** リクエストキー：年 */
	const KEY_YEAR = 'year';
	/** リクエストキー：月 */
	const KEY_MONTH = 'month';
	/** リクエストキー：日 */
	const KEY_DAY = 'day';
	/** リクエストキー：年月日  */
	const KEY_YMD = 'ymd';
	/** リクエストキー：時 */
	const KEY_HOUR = 'hour';
	/** リクエストキー：分 */
	const KEY_MINUTE = 'minute';
	/** リクエストキー：年月日時分 */
	const KEY_YMDHM = 'ymdhm';

	/**
	 * (non-PHPdoc)
	 * @see Lib_Request::get()
	 */
	public function get($sKey)
	{
		/*** 戻り値初期化 */
		$sRet = '';

		/**
		 * リクエストキーによる振り分け
		 */
		switch ($sKey) {
			case self::KEY_YEAR:
				$sRet = $this->_getYear();
				break;
			case self::KEY_MONTH:
				$sRet = $this->_getMonth();
				break;
			case self::KEY_DAY:
				$sRet = $this->_getDay();
				break;
			case self::KEY_HOUR:
				$sRet = $this->_getHour();
				break;
			case self::KEY_MINUTE:
				$sRet = $this->_getMinute();
				break;
			case self::KEY_YMD:
				$sRet = $this->_getYearMonthDay();
				break;
			case self::KEY_YMDHM:
				$sRet = $this->_getYearMonthDayHourMinute();
				break;
			default:
				if ( isset($_REQUEST[$sKey]) ) {
					$sRet = $_REQUEST[$sKey];
				}
				break;
		}
		return $sRet;
	}

	public function getYearMonthDayHourMinute()
	{
		return $this->_getYearMonthDayHourMinute();
	}

	protected function _getYearMonthDayHourMinute()
	{
		$RET = date('YmdHi');

		if ( isset($_REQUEST[self::KEY_YMDHM])) {
			if ( is_numeric($_REQUEST[self::KEY_YMDHM])) {
				if ( strlen($_REQUEST[self::KEY_YMDHM]) == strlen('yyyymmddhhmm')){
					if ( $this->_validYearMonthDayHourMinute() ) {
						$RET = $_REQUEST[self::KEY_YMDHM];
					}
				}
			}
		}
		return $RET;
	}

	protected function _validYearMonthDayHourMinute()
	{
		$bRet = FALSE;
		$sYear = substr($_REQUEST[self::KEY_YMDHM],0,4);
		$sMonth = substr($_REQUEST[self::KEY_YMDHM],4,2);
		$sDay = substr($_REQUEST[self::KEY_YMDHM],6,2);
		$sHour = substr($_REQUEST[self::KEY_YMDHM],8,2);
		$sMinute = substr($_REQUEST[self::KEY_YMDHM],10,2);
		$iHour = sprintf('%d', $sHour);
		$iMinute = sprintf('%d', $sMinute);
		if ( checkdate($sMonth, $sDay, $sYear)) {
			if (($iHour >= 0 ) AND ($iHour <= 23)) {
				if ( ($iMinute >= 0 ) AND ($iMinute <= 59)) {
					$bRet = TRUE;
				}
			}
		}
		return $bRet;
	}

	public function getMinute()
	{
		return $this->_getMinute();
	}
	protected function _getMinute()
	{
		$RET = sprintf('%d', date('i'));

		if ( isset($_REQUEST[self::KEY_MINUTE])) {
			if ( is_numeric($_REQUEST[self::KEY_MINUTE])) {
				if ( ($_REQUEST[self::KEY_MINUTE] >= 0 )
				AND
				( $_REQUEST[self::KEY_MINUTE] <= 50 )) {
					$RET=$_REQUEST[self::KEY_MINUTE];
				}
			}
		}

		return $RET;
	}

	public function getHour()
	{
		return $this->_getHour();
	}
	protected function _getHour()
	{
		$RET = date('G');

		if ( isset($_REQUEST[self::KEY_HOUR])) {
			if ( is_numeric($_REQUEST[self::KEY_HOUR])) {
				if ( ($_REQUEST[self::KEY_HOUR] >= 0 )
				AND
				( $_REQUEST[self::KEY_HOUR] <= 23 )) {
					$RET = $_REQUEST[self::KEY_HOUR];
				}
			}
		}
		return $RET;
	}

	public function getYearFromYMD()
	{
		$sYMD = $this->_getYearMonthDay();
		$sYear = substr($sYMD, 0, strlen('YYYY'));
		return $sYear;
	}

	public  function getMonthFromYMD()
	{
		$sYMD = $this->_getYearMonthDay();
		$sMonth = substr($sYMD, 4, 2);
		$iMonth = sprintf('%d', $sMonth);
		return $iMonth;
	}

	public function getYearMonthDay()
	{
		return $this->_getYearMonthDay();
	}
	protected function _getYearMonthDay()
	{
		$RET = date('Ymd');
		if ( isset($_REQUEST[self::KEY_YMD])) {
			if ( is_numeric($_REQUEST[self::KEY_YMD])) {
				if ( strlen($_REQUEST[self::KEY_YMD]) == strlen('YYYYMMDD')){
					if ( $this->_checkDate() ) {
						$RET = $_REQUEST[self::KEY_YMD];
					}
				}
			}
		}
		return $RET;
	}

	/**
	 * 年・月それぞれのパラメータからYYYYMMを返す
	 * Enter description here ...
	 */
	public function getYYYYMMfromSeparateRequest()
	{
		$sMonth = $this->_getMonth();
		$RET = $this->_getYear() . sprintf('%02d', $sMonth);
		return $RET;
	}

	/**
	 * 年・月・日それぞれのパラメータからYYYYMMDDを返す
	 */
	public function getYYYYMMDDfromSeparateRequest()
	{
		$sMonth = $this->_getMonth();
		$sDay = $this->_getDay();
		$RET = $this->_getYear() . sprintf('%02d', $sMonth) . sprintf('%02d', $sDay);
		return $RET;
	}

	protected function _checkDate()
	{
		$sYear = substr($_REQUEST[self::KEY_YMD], 0, 4);
		$sMonth = substr($_REQUEST[self::KEY_YMD], 4,2);
		$sDay = substr($_REQUEST[self::KEY_YMD], 6,2);
		$bRet = checkdate($sMonth, $sDay, $sYear);
		return $bRet;
	}

	public function getDay()
	{
		return $this->_getDay();
	}
	protected function _getDay()
	{
		$RET = date('j');
		if ( isset($_REQUEST[self::KEY_DAY])) {
			if ( is_numeric($_REQUEST[self::KEY_DAY])) {
				if ( ($_REQUEST[self::KEY_DAY] >= 1)
				AND
					($_REQUEST[self::KEY_DAY] <= 31)
				)
				{
					$RET = $_REQUEST[self::KEY_DAY];
				}
			}
		}
		return $RET;
	}

	public function getYear()
	{
		return $this->_getYear();
	}
	protected function _getYear()
	{
		$RET = date('Y');
		if (isset($_REQUEST[self::KEY_YEAR])) {
			if ( is_numeric($_REQUEST[self::KEY_YEAR])) {
				if ( $_REQUEST[self::KEY_YEAR] >= 1900) {
					$RET = $_REQUEST[self::KEY_YEAR];
				}
			}
		}
		return $RET;
	}

	public function getMonth()
	{
		return $this->_getMonth();
	}
	protected function _getMonth()
	{
		$RET = date('n');
		if ( isset($_REQUEST[self::KEY_MONTH])) {
			if (is_numeric($_REQUEST[self::KEY_MONTH])) {
				if ( ($_REQUEST[self::KEY_MONTH] >= 1)
				AND
				($_REQUEST[self::KEY_MONTH] <=12)
				)
				{
					$RET = $_REQUEST[self::KEY_MONTH];
				}
			}
		}
		return $RET;
	}

	/**
	 * コロン区切りの時刻表示を分割する
	 */
	public static function explodeColonHourMinute($HourColonMinute, $DefaultHour='06', $DefaultMinute='00')
	{
		$aryResult
		= array(
				self::KEY_HOUR=>$DefaultHour,
				self::KEY_MINUTE=>$DefaultMinute
				);
		$aryDivide = explode(':', $HourColonMinute);
		if (isset($aryDivide[0])){
			if (ctype_digit($aryDivide[0])){
				if ($aryDivide[0] < 24){
					$aryResult[self::KEY_HOUR] = $aryDivide[0];
				}
			}
		}
		if (isset($aryDivide[1])){
			if (ctype_digit($aryDivide[1])){
				if ($aryDivide[1] < 60){
					$aryResult[self::KEY_MINUTE] = $aryDivide[1];
				}
			}
		}
		return $aryResult;
	}
}